package com.mooc.house.service;

import com.mooc.house.common.CommonConstants;
import com.mooc.house.common.HouseUserType;
import com.mooc.house.common.PageParams;
import com.mooc.house.common.RestResponse;
import com.mooc.house.mapper.HouseMapper;
import com.mooc.house.model.*;
import com.mooc.house.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HouseService {

    @Autowired
    private FileService fileService;

    @Autowired
    private HouseMapper houseMapper;

    @Value("${file.prefix}")
    private String imgPrefix;

    /**
     * 添加房屋图片
     * 添加户型图片
     * 插入房产信息
     * 绑定用户到房产的关系
     * @param house
     * @param userId
     *
     */
    public RestResponse add(House house, Long userId) {
        house.setState(CommonConstants.HOUSE_STATE_UP);
        houseMapper.insert(house);
        bindUser2House(house.getId(),userId,false);
        return RestResponse.success();
    }


    public void bindUser2House(Long houseId, Long userId, boolean collect) {
        HouseUser existhouseUser =     houseMapper.selectHouseUser(userId,houseId,collect ? HouseUserType.BOOKMARK.value : HouseUserType.SALE.value);
        if (existhouseUser != null) {
            return;
        }
        HouseUser houseUser = new HouseUser();
        houseUser.setHouseId(houseId);
        houseUser.setUserId(userId);
        houseUser.setType(collect ? HouseUserType.BOOKMARK.value : HouseUserType.SALE.value);
        BeanHelper.setDefaultProp(houseUser, HouseUser.class);
        BeanHelper.onInsert(houseUser);
        houseMapper.insertHouseUser(houseUser);
    }


    public ListResponse<House> getOwnList(HouseQueryReq hq) {
        PageParams pageParams = new PageParams();
        pageParams.setLimit(hq.getLimit());
        pageParams.setOffset(hq.getOffset());
        House query = hq.getQuery();

        List<House> houses  = getHouseList(query,pageParams);
        Long houseCount = getHouseCount(query);

        ListResponse listResponse = new ListResponse<House>();
        listResponse.setList(houses);
        listResponse.setCount(houseCount);
        return  listResponse;

    }

    private Long getHouseCount(House query) {
        return houseMapper.selectPageCount(query);
    }

    public List<House> getHouseList(House house,PageParams pageParams) {
      /*  ArrayList<House> houses = Lists.newArrayList();
        if(!Strings.isNullOrEmpty(house.getName())){
            Community community = new Community();
            community.setName(house.getName());
            List<Community> communities = houseMapper.selectCommunity(community);
            if(!communities.isEmpty()){
                house.setCommunityId(communities.get(0).getId());
            }
        }*/
           return  queryAndSetImg(house,pageParams);
}

    public List<House> queryAndSetImg(House query, PageParams pageParams) {
        List<House> houses =   houseMapper.selectPageHouses(query, pageParams);
        houses.forEach(h ->{
            h.setFirstImg(imgPrefix + h.getFirstImg());
            h.setImageList(h.getImageList().stream().map(img -> imgPrefix + img).collect(Collectors.toList()));
            h.setFloorPlanList(h.getFloorPlanList().stream().map(img -> imgPrefix + img).collect(Collectors.toList()));
        });
        return houses;
    }


    public Long selectSaleHouseUser(Long houseId){
        HouseUser houseUser = houseMapper.selectSaleHouseUser(houseId);
        if(houseUser == null && houseUser.getUserId() ==null){
            return null;
        }
        return houseUser.getUserId();
    }


    public void bookMark(HouseUser houseUser) {
        //如果已经存在收藏记录则跳出不用保存
         HouseUser huser = houseMapper.selectHouseUser(houseUser.getUserId(),
                houseUser.getHouseId(), houseUser.getType());
         if(huser!=null){
             return;
         }
          houseMapper.insertHouseUser(houseUser);

    }


    public void unbookMark(HouseUser houseUser) {
        //如果已经存在收藏记录则跳出不用保存
        HouseUser huser = houseMapper.selectHouseUser(houseUser.getUserId(),
                houseUser.getHouseId(), houseUser.getType());
        if(huser==null){
           return;
        }
        houseMapper.deleteHouseUser(houseUser.getUserId(),
                houseUser.getHouseId(), houseUser.getType());
    }


}
